package com.projecetest.eduservice.service.impl;

import com.projecetest.eduservice.entity.EduChapter;
import com.projecetest.eduservice.entity.EduCourse;
import com.projecetest.eduservice.entity.EduCourseDescription;
import com.projecetest.eduservice.entity.EduVideo;
import com.projecetest.eduservice.entity.vo.CourseInfoVo;
import com.projecetest.eduservice.entity.vo.CoursePublishVo;
import com.projecetest.eduservice.mapper.EduCourseMapper;
import com.projecetest.eduservice.service.EduChapterService;
import com.projecetest.eduservice.service.EduCourseDescriptionService;
import com.projecetest.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.projecetest.eduservice.service.EduVideoService;
import com.projecetest.servicebase.exceptionhandler.TestException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author Captain
 * @since 2021-08-04
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService courseDescriptionService;

    @Autowired
    private EduVideoService videoService;

    @Autowired
    private EduChapterService chapterService;

    //添加课程
    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //向课程表添加信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if (insert == 0)
        {
            throw new TestException(20001,"添加课程失败！");
        }
        String cid = eduCourse.getId();
        //向课程简介表添加信息
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setDescription(courseInfoVo.getDescription());
        courseDescription.setId(cid);
        courseDescriptionService.save(courseDescription);

        return cid;
    }

    //根据id查询课程信息
    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        //查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);
        BeanUtils.copyProperties(eduCourse,courseInfoVo);

        //查询课程描述表
        EduCourseDescription courseDescription = courseDescriptionService.getById(courseId);
        courseInfoVo.setDescription(courseDescription.getDescription());

        return courseInfoVo;
    }

    //修改课程信息
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        //修改课程表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int update = baseMapper.updateById(eduCourse);
        if (update == 0)
        {
            throw new TestException(20001,"修改课程失败！");
        }
        //修改课程描述表
        EduCourseDescription courseDescription = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfoVo,courseDescription);
        courseDescriptionService.getById(courseDescription);
    }

    @Override
    public CoursePublishVo publishCourseInfo(String courseId) {
        //调用mapper
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(courseId);
        return publishCourseInfo;
    }

    //删除课程
    @Override
    public void removeCourse(String courseId) {
        //删除小节
        videoService.removeVideoByCourseId(courseId);
        //删除章节
        chapterService.removeChapterByCourseId(courseId);
        //删除描述
        courseDescriptionService.removeById(courseId);
        //删除课程
        int result = baseMapper.deleteById(courseId);
        if (result == 0) {
            throw new TestException(20001, "删除课程失败!");
        }
    }
}
